﻿namespace Homework11
{
    /// <summary>
    /// 在console里输出当月的日历，如图（本日如彩色表示，不要圆圈，不要农历）
    /// </summary>
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Clear();

            //改色
            Console.ForegroundColor = ConsoleColor.Blue;
            //改格式
            Console.WriteLine(DateTime.Today.ToString("yyyy年MM月dd日"));
            //back
            Console.ForegroundColor = ConsoleColor.White;
            //下划线
            Console.WriteLine(new String('_', 40));

            Console.WriteLine("一  二  三  四  五  六  日");

            //Get first day Of this month
            DateTime Firstdayofthismonth = new DateTime(DateTime.Today.Year,
                                             DateTime.Today.Month,
                                             1);
            //Console.WriteLine((int)Firstdayofthismonth.DayOfWeek);
            DateTime firstDayOffirstWeek= Firstdayofthismonth.AddDays(1- (int)Firstdayofthismonth.DayOfWeek);
            //Console.WriteLine(firstDayOffirstWeek);

            //get last day
            DateTime lastDayOfThisMonth= Firstdayofthismonth.AddMonths(1).AddDays(-1);

            //Console.WriteLine((int)(new DateTime(2024, 10, 6)).DayOfWeek);

            //如果是周日就是最后一天，否则还要取所在周的周日
            //周日的Weekday=0,此处要注意
            int weekdayOfLastDayOfLastMonth = (int)(lastDayOfThisMonth.DayOfWeek);
            DateTime lastdayoflastweek;
            if (weekdayOfLastDayOfLastMonth==0)
            {
                //本月最后一天是周日
                lastdayoflastweek = lastDayOfThisMonth;
            }
            else
            {
                lastdayoflastweek=lastDayOfThisMonth.AddDays(7- weekdayOfLastDayOfLastMonth);
            }
            //Console.WriteLine(lastdayoflastweek);
            for (DateTime Thisday= firstDayOffirstWeek; Thisday <=lastdayoflastweek; Thisday=Thisday.AddDays(1)) 
            {
                if (Thisday.Month == DateTime.Today.Month && Thisday.Day == DateTime.Today.Day)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.BackgroundColor = ConsoleColor.Red;
                }
                if (Thisday.Day < 10)
                { Console.Write(" "); }
                Console.Write(Thisday.Day.ToString(""));
                Console.ForegroundColor = ConsoleColor.White;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write("  ");
                if((int)Thisday.DayOfWeek==0)
                    Console.WriteLine();
                

            }







        }
    }
}
